const app = getApp()
//获得全局js变量
var util = require('../../../utils/util.js');
Page({
    data: {
        bankCards: [],
        hasData:false,
        banks: [
            { id: 1, name: '中国工商银行', logo: '/images/bank/icbc.png' },
            { id: 2, name: '中国建设银行', logo: '/images/bank/ccb.png' },
            { id: 3, name: '中国农业银行', logo: '/images/bank/abc.png' },
            { id: 4, name: '中国银行', logo: '/images/bank/boc.png' },
            { id: 5, name: '招商银行', logo: '/images/bank/cmb.png' },
            { id: 6, name: '交通银行', logo: '/images/bank/bocom.png' }
        ]
    },
    onShow: function() {
        this.getBankCards();
    },
    onLoad: function() {
        //this.getBankCards();
    },
    
    // 获取银行卡列表
    getBankCards: function() {
        // 模拟从服务器获取数据
        var that = this;
        util.httpRrquest(app.data.rooturl + 'user/getBankCard', 'GET', { })
        .then(function (response) {
            if (response.data.code == 1) {
                // 确保 bankCards 是一个数组
                const bankCards = Array.isArray(response.data.data) ? response.data.data : [];
                that.setData({
                    bankCards: bankCards
                });
            } else {
                // 处理错误情况，设置空数组
                that.setData({
                    bankCards: []
                });
                console.log('获取银行卡列表失败');
            }
        }, function (error) {
            console.log(error);
            // 处理错误情况，设置空数组
            that.setData({
                bankCards: []
            });
        });
    },
    
    // 添加银行卡
    addBankCard: function() {
        wx.navigateTo({
            url: '/pages/mine/bankcard/add/add',
        });
    },
    
    // 删除银行卡
    deleteBankCard: function(e) {
        const cardId = e.currentTarget.dataset.id;
        
        wx.showModal({
            title: '提示',
            content: '确定要删除该银行卡吗？',
            success: (res) => {
                if (res.confirm) {
                    var that = this;
                    wx.showLoading({
                        title: '删除中...',
                    });
                    
                    util.httpRrquest(app.data.rooturl + 'user/delBankCard', 'GET', { cardId: cardId})
                    .then(function (response) {
                        wx.hideLoading();
                        if (response.data.code == 1) {
                            wx.showToast({
                                title: '删除成功',
                                icon:'success'
                            });
                            that.getBankCards();
                        } else {
                            wx.showToast({
                                title: response.data.msg || '删除失败',
                                icon: 'none'
                            });
                        }
                    }, function (error) {
                        wx.hideLoading();
                        console.log(error);
                        wx.showToast({
                            title: '网络错误，请重试',
                            icon: 'none'
                        });
                    });
                }
                // 用户点击取消，不做任何操作
            }
        });
    },
    
    // 设为默认银行卡
    setDefaultCard: function(e) {
        const cardId = e.currentTarget.dataset.id;
        wx.showModal({
            title: '提示',
            content: '确定要将该银行卡设为默认吗？',
            success: (res) => {
                if (res.confirm) {
                    var that = this;
                    
                    
                    util.httpRrquest(app.data.rooturl + 'user/setDefaultBank', 'GET', { cardId: cardId})
                    .then(function (response) {
                        wx.hideLoading();
                        if (response.data.code == 1) {
                            wx.showToast({
                                title: '成功',
                                icon:'success'
                            });
                            that.getBankCards();
                        }
                    }, function (error) {
                        wx.hideLoading();
                        console.log(error);
                        wx.showToast({
                            title: '网络错误，请重试',
                            icon: 'none'
                        });
                    });
                }
                // 用户点击取消，不做任何操作
            }
        });
    },
    
    // 编辑银行卡
    editBankCard: function(e) {
        const cardId = e.currentTarget.dataset.id;
        // 找到当前要编辑的银行卡数据
        const card = this.data.bankCards.find(item => item.id === cardId);
        
        wx.navigateTo({
            url: `/pages/mine/bankcard/edit/edit?id=${cardId}`,
            success: function(res) {
                // 将银行卡数据传递给编辑页面
                res.eventChannel.emit('acceptCardData', { card: card });
            }
        });
    }
})